/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chat.MsgEvent;

import chat.*;
import java.io.IOException;
import java.net.Socket;
import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author spider391Tang
 */
public class ChatLogin extends EventTransceiver{

    private TalkArea talkArea = null;
    private ChatClient nexthandler = null;
    public ChatLogin(Socket sock, TalkArea talkArea,wbPanel wb,ChatClient nexthandler) {
        super(sock);
        this.talkArea = talkArea;
        this.nexthandler = nexthandler;
    }

    EventObject receiveEvent() throws IOException {
        EventObject ev = null;
        try {

            String rawMsg = in.readLine();
            //System.out.println("==>" + rawMsg);
            String[] names = rawMsg.split(" ");
            String msgType = names[0];
            /// 公子乖 -　Parse 參數
            if (msgType.equals("/show")) {
                ev = new ShowEvent(rawMsg.substring(5), ShowEvent.NEWLINE);
            } else if (msgType.equals("/LoginOK")) {
                ev = new LoginOKEvent(rawMsg.substring(9));
            } 
        } catch (ClassCastException e) {
            System.out.println("Non-event object sent to EventTransceiver");
        }
        return ev;
    }


    public void handleEvent(EventObject e) {
          if (e instanceof LoginOKEvent) {
              System.out.println((String) e.getSource());
              talkArea.setText("");
              this.leave();
              this.nexthandler.start();
              this.nexthandler.tell_server_start();
          }else   if (e instanceof ShowEvent) {            
              talkArea.append((String) e.getSource()+"\n");
          }
    }

}
